#!/bin/bash

# 零售平台Docker Compose状态检查脚本
# 作者: retail
# 日期: 2024-01-01

set -e

echo "🔍 检查零售平台Docker Compose环境状态..."

# 进入项目根目录
cd "$(dirname "$0")/.."

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker未运行"
    exit 1
fi

echo "✅ Docker正在运行"

# 检查服务状态
echo ""
echo "📊 服务状态："
docker compose -f docker/docker-compose.yml -p retail-platform ps

# 检查服务健康状态
echo ""
echo "🏥 健康检查："

# 检查后端健康状态
echo -n "🔧 后端服务: "
if curl -s -f http://localhost:8080/retail-admin/api/auth/health > /dev/null 2>&1; then
    echo "✅ 健康"
else
    echo "❌ 不健康"
fi

# 检查前端健康状态
echo -n "🌐 前端服务: "
if curl -s -f http://localhost:3000 > /dev/null 2>&1; then
    echo "✅ 健康"
else
    echo "❌ 不健康"
fi

# 检查Nginx健康状态
echo -n "🌍 Nginx代理: "
if curl -s -f http://localhost:80 > /dev/null 2>&1; then
    echo "✅ 健康"
else
    echo "❌ 不健康"
fi

# 检查MySQL连接
echo -n "🗄️  MySQL数据库: "
if docker compose -f docker/docker-compose.yml -p retail-platform exec -T mysql mysqladmin ping -h localhost --silent; then
    echo "✅ 健康"
else
    echo "❌ 不健康"
fi

# 检查Redis连接
echo -n "📦 Redis缓存: "
if docker compose -f docker/docker-compose.yml -p retail-platform exec -T redis redis-cli ping > /dev/null 2>&1; then
    echo "✅ 健康"
else
    echo "❌ 不健康"
fi

echo ""
echo "📋 访问地址："
echo "  🌐 前端应用: http://localhost:3000"
echo "  🔧 后端API: http://localhost:8080"
echo "  🌍 Nginx代理: http://localhost:80"
echo "  🗄️  MySQL: localhost:3306"
echo "  📦 Redis: localhost:6379"

echo ""
echo "📝 查看日志："
echo "  docker compose -f docker/docker-compose.yml -p retail-platform logs -f [service_name]"
